package CI;


public class ConsCell {
	private Object _first,_second;
	public ConsCell(Object firstVal, Object secondVal) {
		_first = firstVal;
		_second = secondVal;
	}
	public Object getFirst() {
		return _first;
	}
	public Object getSecond() {
		return _second;
	}
	public String strReturn() throws EvalError {
		if (_second == null) 
			return _first.toString();
		else if (_second.getClass().equals(ConsCell.class)) {
			ConsCell cdr = (ConsCell) _second;
			return _first.toString() + " " + cdr.strReturn();
		}
		else 
			throw new EvalError("strReturn expected cell to be a list");
	}
	public String toString() {
		if (ListTest(this)) {
			try {
				return "(" + strReturn() + ")";
			} catch (EvalError e) {				
				return "error";
			}		
		}
		else {
			return "(" + _first.toString() + " . " + _second.toString() + ")"; 
		}
	}
	public static boolean ListTest(Object c) {
		if (!(c instanceof ConsCell)) {
			return false;
		}
		if (((ConsCell)c).getSecond() == null) {
			return true;
		}
		return ListTest(((ConsCell)c).getSecond());
	}
}
